package com.drug.framework.aspectj;

import com.drug.common.annotation.NeedCheck;
import com.drug.common.utils.valid.ParamCheckUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * @author Alexlyn
 * @Date 2021/12/10 10:00
 * @Description: 参数校验Aspect
 * @Version 1.0
 */
@Aspect
@Order
@Component
public class ValidParamAspect {


    @Pointcut("@within(com.drug.common.annotation.NeedCheck)")
    public void dsPointCut() {

    }

    @Around(value = "dsPointCut()")
    public Object isValid(ProceedingJoinPoint point) throws Throwable {
        valid(point.getArgs(), AnnotationUtils.findAnnotation(point.getTarget().getClass(), NeedCheck.class));
        return point.proceed();
    }

    private void valid(Object[] objects, NeedCheck needCheck) {
        for (Object object : objects) {
            ParamCheckUtils.validate(object, needCheck.value());
        }
    }

}
